import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import LoginView from "../views/LoginView.vue";
import ListView from "../views/ListView.vue";
import RegisterView from "../views/RegisterVue.vue";
import AdminView from "../views/AdminView.vue";
import ShopView from "../views/ShoplistViwe.vue";
import AddshopView from "../views/AddshopView.vue";
import HomeView from "../views/HomeView.vue";
import AddLunboView from "../views/AddLunboView.vue";
import LunboView from "@/views/LunboView.vue";

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            redirect: "/list",
        },
        {
            path: "/login",
            name: "login",
            component: LoginView,
        },
        {
            path: "/register",
            name: "register",
            component: RegisterView,
            meta: { requireLogin: true },
        },

        {
            path: "/list",
            name: "list",
            component: ListView,
            redirect: "/list/home",
            meta: { requireLogin: true },
            children: [
                {
                    path: "/list/home",
                    name: "home",
                    component: HomeView,
                    meta: { requireLogin: true },
                },
                {
                    path: "/list/adminmange",
                    name: "adminmange",
                    component: AdminView,
                    meta: { requireLogin: true },
                },
                {
                    path: "/list/shop",
                    name: "shop",
                    component: ShopView,
                    meta: { requireLogin: true },
                },
                {
                    path: "/list/add",
                    name: "addshop",
                    component: AddshopView,
                    meta: { requireLogin: true },
                },
                {
                    path: "/list/addlunbo",
                    name: "addlunbo",
                    component: AddLunboView,
                    meta: { requireLogin: true },
                },
                {
                    path: "/list/lunbomanage",
                    name: "lunbomanage",
                    component: LunboView,
                    meta: { requireLogin: true },
                },
            ],
        },
    ],
});
router.beforeEach((to, from) => {
    if (to.meta.requireLogin === true) {
        //需要登录
        let token = localStorage.getItem("token");
        if (token == null) {
            return { path: "/login" };
        }
    } else {
        // 不需要登录
    }
});

export default router;
